// index.js
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'

Page({
  data: {
    motto: 'Hello World',
    userInfo: {
      avatarUrl: defaultAvatarUrl,
      nickName: '',
    },
    hasUserInfo: false,
    canIUseGetUserProfile: wx.canIUse('getUserProfile'),
    canIUseNicknameComp: wx.canIUse('input.type.nickname'),
    silentLoginCode: '',
    silentLoginCodeDisplay: '',
    phoneLoginCode: '',
    phoneLoginCodeDisplay: '',
    phoneData: {
      agreed: false,
      code: '',
      codeDisplay: '',
      encryptedData: '',
      encryptedDataDisplay: '',
      iv: '',
      ivDisplay: ''
    }
  },
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onChooseAvatar(e) {
    const { avatarUrl } = e.detail
    const { nickName } = this.data.userInfo
    this.setData({
      "userInfo.avatarUrl": avatarUrl,
      hasUserInfo: nickName && avatarUrl && avatarUrl !== defaultAvatarUrl,
    })
  },
  onInputChange(e) {
    const nickName = e.detail.value
    const { avatarUrl } = this.data.userInfo
    this.setData({
      "userInfo.nickName": nickName,
      hasUserInfo: nickName && avatarUrl && avatarUrl !== defaultAvatarUrl,
    })
  },
  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res)
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },
  silentLogin() {
    wx.login({
      success: (res) => {
        if (res.code) {
          this.setData({
            silentLoginCode: res.code,
            silentLoginCodeDisplay: this.formatCode(res.code)
          });
        } else {
          wx.showToast({
            title: '登录失败',
            icon: 'error'
          });
        }
      }
    });
  },
  getPhoneNumber(e) {
    if (e.detail.errMsg === 'getPhoneNumber:ok') {
      this.setData({
        'phoneData.agreed': true,
        'phoneData.code': e.detail.code,
        'phoneData.codeDisplay': this.formatCode(e.detail.code),
        'phoneData.encryptedData': e.detail.encryptedData || '',
        'phoneData.encryptedDataDisplay': this.formatCode(e.detail.encryptedData || ''),
        'phoneData.iv': e.detail.iv || '',
        'phoneData.ivDisplay': this.formatCode(e.detail.iv || '')
      });
    } else {
      wx.showToast({
        title: '用户拒绝授权',
        icon: 'none'
      });
    }
  },
  copyCode(e) {
    const code = e.currentTarget.dataset.code;
    if (code) {
      wx.setClipboardData({
        data: code,
        success: () => {
          wx.showToast({
            title: '复制成功',
            icon: 'success'
          });
        }
      });
    }
  },
  formatCode(code) {
    if (!code) return '';
    if (code.length > 10) {
      return code.substring(0, 10) + '...';
    }
    return code;
  },
  onLoad() {
    this.silentLogin();
  }
})
